﻿using Bright.NetCore.Api.Helpers;
using Framework.EncryptionDecryptionService.Achieve;
using Framework.EncryptionDecryptionService.Attributes;
using Framework.EncryptionDecryptionService.Enums;
using Framework.EncryptionDecryptionService.Inteface;
using Framework.EncryptionDecryptionService.Models;
using Microsoft.AspNetCore.Mvc;

namespace Bright.NetCore.Api.Controllers
{
    [ApiController]
    [Route("api/Secret/[action]")]
    public class ObtainTheSecretKeyController : ControllerBase
    {

        /// <summary>
        /// RES加密服务
        /// </summary>
        private readonly IEncryptionDecryptionService encryptionRSA;
        public ObtainTheSecretKeyController(IEncryptionDecryptionService encryption)
        {
            encryptionRSA = encryption;
        }

        /// <summary>
        /// 获取RSA公钥
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NonEncrypted(EncryptionType.Response)]
        public string GetRSAEncryptionInfo()
        {
            return encryptionRSA.EncryptionService(EncryptionAlgorithm.RSA).GetSecretKey.PublicSecretKey;
        }

        /// <summary>
        /// 获取AES秘钥
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NonEncrypted]
        public SecretKeyModel WebGetAesSecretKey()
        {
            EncryptionServiceAes aes = new EncryptionServiceAes();
            return aes.GetSecretKey;
        }

        /// <summary>
        /// RSA加密
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NonEncrypted]
        public object RSAEncryption(object entity)
        {
            return encryptionRSA.EncryptionService(EncryptionAlgorithm.RSA).Encrypt(entity);
        }

        /// <summary>
        /// AES加密
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NonEncrypted(EncryptionType.Response)]
        public object WebAESEncryption(object data)
        {
            return data;
        }

        /// <summary>
        /// RSA解密
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NonEncrypted]
        public object RSADecrypt([FromBody] object body)
        {
            return encryptionRSA.DecryptionService(EncryptionAlgorithm.RSA).Decrypt(body.ToString());
        }

        /// <summary>
        /// AES解密
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        [NonEncrypted(EncryptionType.Request)]
        public object WebAESDecrypt(object data)
        {
            return data;
        }
    }
}
